#!/bin/bash

# merge-info.sh
# 
# Script to merge the results of several log and list files into one.
#
# Syntax
#
#    ./merge-info.sh [opts] log-server.txt log-mta.txt
#
#    log-server.txt is a text file produced by server-track.sh
#    log-mta.txt is a text file produced by mta-track.sh
#
# Options
#
#    -F fe-inst.txt 
#      the list of FE institutions produced by get-institution-from-domain.sh
#
#    -H he-inst.txt 
#      the list of HE institutions produced by get-institution-from-domain.sh
#
#    -h
#      this help
#
# Output lines have these formats:
#
# "unknown" \t "not registered" \t domain \t ...
#   ... "host not found for domain"
#   ... "no MX record"
#   ... server \t "server not found"
#   ... server \t "no MTA track"
#   ... server \t "unknown MTA"
#   ... server \t tag \t telnet line
#
# "fe"/"he"/"none" \t inst \t domain \t ...
#   ... "host not found for domain"
#   ... "no MX record"
#   ... server \t "server not found"
#   ... server \t "no MTA track"
#   ... server \t "unknown MTA"
#   ... server \t tag \t telnet line
#
# Results are sent to stdout.
#
# v0.3  20 Sep 2007
#
# Author: Ramon Casero <ramon.casero@oucs.ox.ac.uk>
# Copyright © 2007 University of Oxford
#
# This file is part of mta-tracker.
#
# mta-tracker is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mta-tracker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with mta-tracker.  If not, see <http://www.gnu.org/licenses/>.
#
# ChangeLog
#
# * 2008-08-21 (rcasero):
#
#   ** Upgrade licence to GPL v3.
#
# * 2007-09-20 (rcasero): v0.3.
#
#   ** Fix bug. When tagging institutions as fe/he/none, it is
#   necessary to check for institutions names from the beginning of
#   the line; otherwise institutions with partial matches are thought
#   to be FE or HE.
#
# * 2007-09-07 (rcasero): v0.2.
#
#   ** Add "no server" tag to field 6 for the cases where it would be
#   empty, i.e. when the domain doesn't have a host or an MX record.
#
# * 2007-08-30 (rcasero): v0.1. First running version.

# syntax message
usage() {
    echo "Syntax: `basename $0` [opts] log-server.txt log-mta.txt"
    echo
    echo "log-server.txt"
    echo "  the log file produced by server-track.sh"
    echo
    echo "log-mta.txt"
    echo "  the log file produced by mta-track.sh"
    echo
    echo "Options"
    echo
    echo "  -F fe-inst.txt"
    echo "    the list of FE institutions produced by get-institution-from-domain.sh"
    echo
    echo "  -H he-inst.txt"
    echo "    the list of HE institutions produced by get-institution-from-domain.sh"
    echo
    echo "  -h"
    echo "    this help"
}

# parse option arguments
mtatags=
felist=
helist=
while getopts  "hT:F:H:" option
do
  # parse options
  case "$option" in
      h) # help
	  usage
	  exit 0
	  ;;
      F) # file with list of FE institutions
	  felist="$OPTARG"
	  if [ -e "$felist" ]
	      then
	      echo FE institutions file: "$felist" 1>&2
	  else
	      echo Error! File cannot be found: "$felist" 1>&2
	      exit 1
	  fi
	  ;;
      H) # file with list of HE institutions
	  helist="$OPTARG"
	  if [ -e "$helist" ]
	      then
	      echo HE institutions file: "$helist" 1>&2
	  else
	      echo Error! File cannot be found: "$helist" 1>&2
	      exit 1
	  fi
	  ;;
  esac
done

# get ready to process non-option arguments (i.e. file names)
shift $((OPTIND-1)); OPTIND=1

# check number of input arguments
if [ ! $# -eq 2 ]
    then
    usage
    exit 1
fi

# name input arguments
slog="$1"
mlog="$2"

# check that input files exists
if [ -e "$slog" ]
    then
    echo Server log file: "$slog" 1>&2
else
    echo Error! File cannot be found: "$slog" 1>&2
    exit 1
fi
if [ -e "$mlog" ]
    then
    echo MTA log file: "$mlog" 1>&2
else
    echo Error! File cannot be found: "$mlog" 1>&2
    exit 1
fi

# # if domain is not registered, it has no institution name and no
# # servers, so nothing can be done
# egrep "^not registered" "$slog" | awk '{print "none\t", $0}'

# exit 0

while read sline
do

  # add none/fe/he tags to the server log line
  case "$sline" in

      # no institution identified for domain, add "unknown" tag
      not\ registered*)
      sline="unknown	$sline"
      ;;
      
      # if an institution was identified
      *)

	  # institution tag
	  inst=$(echo "$sline" | awk 'BEGIN {FS="\t"}{print $1}')

	  # he/fe tags, if we have an HE/FE institution list
	  if [ `grep -c "^$inst"'$' "$helist"` -gt 0 ]
	      then
	      sline="he	$sline"
	  elif [ `grep -c "^$inst"'$' "$felist"` -gt 0 ]
	      then
	      sline="fe	$sline"
	  else
	      sline="none	$sline"
	  fi
	  ;;
  esac

  # server name
  serv=$(echo "$sline" | awk 'BEGIN {FS="\t"}{print $4}')

  # add MTA info if available
  case "$serv" in
      host\ not\ found\ for\ domain)
      sline="$sline	no server"
      ;;

      no\ MX\ record)
      sline="$sline	no server"
      ;;

      *)
	  # find server's MTA information in mta log, and remove
	  # server name from that line
	  mline=`egrep -m 1 '^'"$serv" "$mlog" | cut -f 2-`

	  # update output line
	  if [[ "$mline" == "" ]]
	      then	      
	      sline="$sline	no MTA track"
	  else
	      sline="$sline	$mline"
	  fi
	  ;;
  esac

  echo "$sline"

done < <(cat "$slog")
